﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GoogleAppsTools
{
    public partial class NewEvent : Form
    {
        public NewEvent()
        {
            InitializeComponent();
            allDayEventCheckBox.Checked = true;
            startHour.Text = DateTime.Now.Hour.ToString();
            startMin.Text = findMultiplicationOfFive(DateTime.Now.Minute);
            endHour.Text = DateTime.Now.AddHours(1).Hour.ToString();
            endMin.Text = findMultiplicationOfFive(DateTime.Now.Minute);
        }

        private string findMultiplicationOfFive(int number)
        {
            while (number % 5 != 0)
            {
                number++;
            }
            return number.ToString();
        }

        private void CreateButton_Click(object sender, EventArgs e)
        {
            DateTime startTime;
            DateTime endTime;
            GoogleEventsSyncDataSource GoogleAccount = new GoogleEventsSyncDataSource();

            //this if statement checks to see if the event is all day.
            //If not, it uses both the date and time picker to create a DateTime object.
            //if the event is all day, just the date picker is used.
            if (allDayEventCheckBox.Checked != true)
            {
                startTime = Convert.ToDateTime(StartTimeDatePicker.Value.ToShortDateString() + " " + startHour.Text.ToString() + ":" + startMin.Text.ToString());
                endTime = Convert.ToDateTime(EndTimeDatePicker.Value.ToShortDateString() + " " + endHour.Text.ToString() + ":" + endMin.Text.ToString());
            }
            else
            {
                startTime = StartTimeDatePicker.Value;
                endTime = EndTimeDatePicker.Value;
            }

            //the Generic event object is then created with the form info and sent to the google servers
            GenericEvent newEvent = new GenericEvent() { Title = titleTextBox.Text, Contents = DescriptionTextBox.Text, StartTime = startTime, EndTime = endTime, Location = LocationTextBox.Text, AllDay = allDayEventCheckBox.Checked };
            GoogleAccount.WriteItem(newEvent);

            //the form text boxes are then cleared
            titleTextBox.Text = "";
            DescriptionTextBox.Text = "";
            LocationTextBox.Text = "";
            this.Close();
        }

        private void allDayEventCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            //if the all day checkbox is selected, the time pickers are disabled.
            if (allDayEventCheckBox.Checked == true)
            {
                startHour.Enabled = false;
                startMin.Enabled = false;
                endHour.Enabled = false;
                endMin.Enabled = false;

            }
            else
            {
                startHour.Enabled = true;
                startMin.Enabled = true;
                endHour.Enabled = true;
                endMin.Enabled = true;
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            titleTextBox.Text = "";
            DescriptionTextBox.Text = "";
            LocationTextBox.Text = "";
            this.Close();
        }
    }
}
